package com.cxb.utils;

import android.text.TextUtils;
import android.util.Log;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Debug {
	
	
	public static final boolean DEBUG = false;
	private static DateFormat formatter = new SimpleDateFormat("MM/dd HH:mm:ss");
	
	
	public static void printf(String tag, String msg) {

		if (tag != null && msg != null) {
			Log.i(tag, msg);
		}
	}

	private static FileOutputStream fos;

	public static void printfGpsLog(String msg) {
		// try {
		// if (fos == null) {
		// fos = new FileOutputStream("/mnt/sdcard/cxb_gps.log",true);
		// }
		// fos.write((msg + "\n").getBytes());
		// fos.flush();
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
	}

	public static void printfNemaLog(String msg) {
		// try {
		// if (fos == null) {
		// fos = new FileOutputStream("/mnt/sdcard/nema.log", true);
		// }
		// fos.write((msg + "\n").getBytes());
		// fos.flush();
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
	}

	public static void printfGPSLog(String content) {
		if (DEBUG) {
			try {
				FileWriter writer = new FileWriter(
						"/mnt/sdcard/gpsActions.log", true);
				StringBuilder sb = new StringBuilder();
				sb.append(content);
				sb.append(" --- ");
				sb.append(formatter.format(new Date()));
				sb.append('\n');
				writer.write(sb.toString());
				writer.flush();
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public static void printfLog(String content) {
		if(TextUtils.isEmpty(content)){
			return;
		}
		if (DEBUG) {
			try {
				FileWriter writer = new FileWriter(
						"/mnt/sdcard/ami.log", true);
				StringBuilder sb = new StringBuilder();
				sb.append(content);
				sb.append(" --- ");
				sb.append(formatter.format(new Date()));
				sb.append('\n');
				writer.write(sb.toString());
				writer.flush();
				writer.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		Log.i("Debug", content);
	}
	public static void printfLog(Throwable ex) {
		if (DEBUG) {
			try {
				PrintWriter writer = new PrintWriter(new FileWriter(
						"/mnt/sdcard/ami.log", true));
				ex.printStackTrace(writer);
				Throwable cause = ex.getCause();
				while (cause != null) {
					cause.printStackTrace(writer);
					cause = cause.getCause();
				}
				writer.flush();
				writer.close();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}
	
	public static void upload(String content) {
		if (DEBUG) {
			try {
				FileWriter writer = new FileWriter(
						"/mnt/sdcard/upload.log", true);
				StringBuilder sb = new StringBuilder();
				sb.append(content);
				sb.append(" --- ");
				sb.append(formatter.format(new Date()));
				sb.append('\n');
				writer.write(sb.toString());
				writer.flush();
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	}
	
	public static void tempLog(String content) {
		if (DEBUG) {
			try {
				Log.d("pumkid", content);
				FileWriter writer = new FileWriter(
						"/mnt/sdcard/temp.log", true);
				StringBuilder sb = new StringBuilder();
				sb.append(content);
				sb.append(" --- ");
				sb.append(formatter.format(new Date()));
				sb.append('\n');
				writer.write(sb.toString());
				writer.flush();
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	}
}
